﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Models;
namespace WebHoiChoMuaBan.Controllers.Private
{
    public class MessageController : PrivateController
    {
        private void Init()
        {
            ViewData["mid"] = "message";
            LoadTimeAndCurrency();
        }

        public ActionResult List(int? uid, int? page)
        {
            if (uid == null)
                return HomePage();
            if (LoadMember(uid.Value) == null)
            {
                Init();
                //ViewData["page"] = page;
                return View("~/Views/Private/MessageList.aspx");
            }
            return HomePage();
        }

        //
        // GET: /Message/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Message/MakeFriend

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Message/MakeFriend

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        public RedirectResult SendMessage(FormCollection collection)
        {
            
            string content = (string)collection["txtComment"];
            if (content == null)
            {
                content = (string)collection["txtCommentreply"];
            }
            ChatHelper chat = new ChatHelper();
            List<ChatEmotionDetail> emos = chat.getEmotionPath();
            foreach (ChatEmotionDetail emo in emos)
            {
                content = content.Replace(emo.notation, "<img src='" + emo.path + "' width='40px' height='40px'/>");
            }
            int memberId = int.Parse(collection["MemId"] + "");
            if (collection["IsReplyTo"] != null)
                new ChatHelper().AddMsgToMember(int.Parse(Session["MemId"] + ""), memberId, int.Parse(collection["isReplyTo"] + ""), content);
            else
                new ChatHelper().AddMsgToMember(int.Parse(Session["MemId"] + ""), memberId, null, content);
            return Redirect(Request.UrlReferrer.AbsoluteUri + "#comments");
        }

        //
        // GET: /Message/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Message/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Message/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Message/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
